package en.edu.zxj.rabbit.extensions.demo.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-21
 * @time: 22:27:01
 */
@Component
public class qosQueueListener {
    @RabbitListener(queues = "qosQueue")
    public void qosQueueListener(Message message, Channel channel) throws IOException, InterruptedException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("[1]接收到消息: %s, deliveryTag: %d%n", new
                String(message.getBody(), StandardCharsets.UTF_8), deliveryTag);
        // 手动确认消息
        Thread.sleep(50);
        channel.basicAck(deliveryTag,  true);
    }

    @RabbitListener(queues = "qosQueue")
    public void qosQueueListener2(Message message, Channel channel) throws IOException, InterruptedException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("[2]接收到消息: %s, deliveryTag: %d%n", new
                String(message.getBody(), StandardCharsets.UTF_8), deliveryTag);
        // 处理业务, sleep 模拟该消费者消费的速度慢
        Thread.sleep(100);
        // 手动确认消息
        channel.basicAck(deliveryTag,  true);
    }
}
